#include "ruleitemmodel.h"
#include <QFile>

RuleItemModel::RuleItemModel(QObject *parent) :
    QAbstractItemModel(parent)
{
    setupModel();
}

void RuleItemModel::setupModel()
{
    QFile file(PRINTRULE_CFG);
    if (file.open(QIODevice::ReadOnly)) {
        QDataStream in(&file);
        while (!in.atEnd()) {
            PrintRule *rule = new PrintRule;
            in >> *rule;
            ruleList << rule;
        }
    }
}

void RuleItemModel::getRulesFromFile(QList<PrintRule *> &rules)
{
    RuleItemModel model;
    model.getRuleList(rules);
}

bool RuleItemModel::saveData()
{
    QFile file(PRINTRULE_CFG);
    if (file.open(QIODevice::WriteOnly)) {
        QDataStream out(&file);
        foreach (PrintRule *rule, ruleList) {
            out << *rule;
        }
        file.close();
        return true;
    } else return false;
}

QModelIndex RuleItemModel::index(int row, int column, const QModelIndex &parent) const
{
    if (!hasIndex(row, column, parent) || parent.isValid())
        return QModelIndex();

    return createIndex(row, column, ruleList.at(row));
}

QVariant RuleItemModel::data(const QModelIndex &index, int role) const
{
    if (index.isValid()) {
        PrintRule *rule = static_cast<PrintRule*>(index.internalPointer());
        if (Qt::DisplayRole == role) {
            switch (index.column()) {
            case AETitle:
                return rule->aetitle;
            case Printer:
                return rule->printer;
            case EmptyImage:
                return densityType2String(rule->emptyImage);
            case Border:
                return densityType2String(rule->border);
            case TrueSize:
                return rule->trueSize?tr("Yes"):tr("No");
            default:
                return QVariant();
            }
        } else return QVariant();
    } else return QVariant();
}

bool RuleItemModel::setData(const QModelIndex &index, const QVariant &value, int role)
{
    if (index.isValid() && Qt::EditRole == role) {
        PrintRule *rule = static_cast<PrintRule*>(index.internalPointer());
        switch (index.column()) {
        case AETitle:
            rule->aetitle = value.toString();
            break;
        case Printer:
            rule->printer = value.toString();
            break;
        case EmptyImage:
            rule->emptyImage = (DensityType)value.toInt();
            break;
        case Border:
            rule->border = (DensityType)value.toInt();
            break;
        case TrueSize:
            rule->trueSize = value.toInt();
            break;
        }
        emit dataChanged(index, index, QVector<int>()<<Qt::DisplayRole);
        return true;
    } else return false;
}

QVariant RuleItemModel::headerData(int section, Qt::Orientation orientation, int role) const
{
    if (Qt::DisplayRole == role) {
        if (Qt::Horizontal == orientation) {
            switch (section) {
            case AETitle:
                return tr("AE Title");
                break;
            case Printer:
                return tr("Printer");
                break;
            case EmptyImage:
                return tr("Empty Image");
                break;
            case Border:
                return tr("Border");
                break;
            case TrueSize:
                return tr("True Size");
                break;
            default:
                return QVariant();
            }
        } else return section+1;
    } else return QVariant();
}

bool RuleItemModel::insertRows(int row, int count, const QModelIndex &parent)
{
    if (!parent.isValid()) {
        beginInsertRows(parent, row, row+count-1);
        for (int i = 0; i < count; ++i) {
            PrintRule *rule = new PrintRule;
            ruleList.insert(row+i, rule);
        }
        endInsertRows();
        return true;
    } else return false;
}

bool RuleItemModel::removeRows(int row, int count, const QModelIndex &parent)
{
    if (!parent.isValid()) {
        beginRemoveRows(parent, row, row+count-1);
        for (int i = 0; i < count; ++i) {
            delete ruleList.takeAt(row);
        }
        endRemoveRows();
        return true;
    } else return false;
}

Qt::ItemFlags RuleItemModel::flags(const QModelIndex &index) const
{
    return Qt::ItemIsEnabled | Qt::ItemIsEditable | Qt::ItemIsSelectable;
}
